package app.softtech.freechat.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import app.softtech.freechat.ActivityFriendRequest;
import app.softtech.freechat.ActivitySearchFriendOnline;
import app.softtech.freechat.Constants;
import app.softtech.freechat.R;
import app.softtech.freechat.adapter.CustomListViewAdapter;
import app.softtech.freechat.database.ConstantsDB;
import app.softtech.freechat.database.DatabaseFriend;
import app.softtech.freechat.database.DatabaseHanler;

public class FriendListFragment extends Fragment {
	static final String TAG = FriendListFragment.class.getSimpleName();
	
	private TextView mNoSearchResultTextView;
	private ListView mFriendListView;
	private Button mSearchFriendOline, btnTest;
	
	//Adapter for list view
	private CustomListViewAdapter mAdapter;
	
	//Database for list view
	private List<DatabaseFriend> mFriendList;
	
	//Copy of mFriendList
	private ArrayList<DatabaseFriend> arrayList;
	
	private String userId;
	
	static String resultSearch = "";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_friendlist_or_recent, container, false);
		
		setHasOptionsMenu(true);
		
		userId = getActivity().getSharedPreferences(Constants.PROJECT_PREFS, Context.MODE_PRIVATE).getString(Constants.USER_ID, "");
		
//		getActivity().deleteDatabase(ConstantsDB.DATABASE_NAME);
		DatabaseHanler db = new DatabaseHanler(getActivity().getApplicationContext());
		
		//For test

		mFriendList = db.getAllFriendByUserId(Integer.parseInt(userId));
		arrayList = new ArrayList<DatabaseFriend>();
		arrayList.addAll(mFriendList);		
		mNoSearchResultTextView = (TextView) rootView.findViewById(R.id.tv_no_result_search_hint);
		mFriendListView = (ListView) rootView.findViewById(R.id.lv_friendlist_or_recent);
		mSearchFriendOline = (Button) rootView.findViewById(R.id.btn_search_friend_online);
		mAdapter = new CustomListViewAdapter(getActivity(), mFriendList);
		btnTest = (Button) rootView.findViewById(R.id.btn_for_test_2);
				
		
		mNoSearchResultTextView.setVisibility(View.GONE);
		mSearchFriendOline.setVisibility(View.GONE);
		btnTest.setVisibility(View.GONE);
		
		mFriendListView.setAdapter(mAdapter);
		
		if(!resultSearch.isEmpty()){
			mFriendList.clear();
			mFriendList.addAll(arrayList);
		}
		
		mAdapter.notifyDataSetChanged();
		
		mSearchFriendOline.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString(Constants.USERNAME, resultSearch);
				Intent i = new Intent(getActivity(), ActivitySearchFriendOnline.class);
				i.putExtras(bundle);
				startActivity(i);
			}
		});
		
		btnTest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(), ActivityFriendRequest.class);
				startActivity(i);
			}
		});
		return rootView;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.main, menu);
		SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
		searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

		searchView.setOnQueryTextListener(new OnQueryTextListener() {

			@Override
			public boolean onQueryTextSubmit(String query) {
				resultSearch = query;
				return true;
			}
			
			//Update the list view when search view text changes 
			@Override
			public boolean onQueryTextChange(String newText) {
				if(!newText.isEmpty()){
					resultSearch = newText;
					mFriendList.clear();
					for(DatabaseFriend df : arrayList){
						if(df.getFriend_Name().toLowerCase(Locale.getDefault()).contains(newText)){
							mFriendList.add(df);
						}
					}
					if(mFriendList.isEmpty()){
						mNoSearchResultTextView.setVisibility(View.VISIBLE);
						mSearchFriendOline.setVisibility(View.VISIBLE);
						btnTest.setVisibility(View.VISIBLE);
					} else {
						mNoSearchResultTextView.setVisibility(View.GONE);
						mSearchFriendOline.setVisibility(View.GONE);
						btnTest.setVisibility(View.GONE);
					}
				} else {
					mNoSearchResultTextView.setVisibility(View.GONE);
					mSearchFriendOline.setVisibility(View.GONE);
					btnTest.setVisibility(View.GONE);
					mFriendList.clear();
					mFriendList.addAll(arrayList);
				}
				
				mAdapter.notifyDataSetChanged();
				Log.i("FragmentFriendList", newText);
				return false;
			}
		});
	}
	
	@Override
	public void onStart() {
		super.onStart();
		mNoSearchResultTextView.setVisibility(View.GONE);
		mSearchFriendOline.setVisibility(View.GONE);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mNoSearchResultTextView.setVisibility(View.GONE);
		mSearchFriendOline.setVisibility(View.GONE);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}
}
